#include </home/david/Escritorio/Embebidos/OpenCV-2.4.2/modules/objdetect/include/opencv2/objdetect/objdetect.hpp>
#include </home/david/Escritorio/Embebidos/OpenCV-2.4.2/modules/highgui/include/opencv2/highgui/highgui.hpp>
#include </home/david/Escritorio/Embebidos/OpenCV-2.4.2/modules/imgproc/include/opencv2/imgproc/imgproc.hpp>
//#include <iostream>
#include <stdio.h>
#include <string.h>
#include "cv.h"
IplImage* GetThresholdedImage(IplImage* imgHSV){       
       IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
       cvInRangeS(imgHSV, cvScalar(75,90,80,0), cvScalar(130,255,170,0), imgThresh);//verde min 50 max 70
       return imgThresh;
}

int main(){
	CvCapture* camera = cvCreateCameraCapture(-1); // Uso de la camara por default
	cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Reconocimiento", CV_WINDOW_AUTOSIZE);


	IplImage* frame;                              //
	if (!camera){
		printf("Fallo captura\n");
		return -1;
	}
	while(1){
		frame = cvQueryFrame(camera);
		//if(!frame) break;
		
		frame=cvCloneImage(frame);
		cvSmooth(frame,frame, CV_GAUSSIAN, 3, 3, 0, 0);
		IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
		cvCvtColor(frame, imgHSV, CV_BGR2HSV);
		IplImage* imgThresh = GetThresholdedImage(imgHSV);
		cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3,0,0);
		

		cvShowImage("Video", frame);
		cvShowImage("Reconocimiento", imgThresh);

		//cvReleaseCapture(&imgThresh);
		//cvReleaseCapture(&imgHSV);
		//cvReleaseCapture(&frame);
		//cvReleaseCapture(&camera);
		char c = cvWaitKey(10);//33
		if(c==27) break;//30
	}
	//cvReleaseCapture(&camera);
	cvDestroyAllWindows();
	cvReleaseCapture(&camera);
	return 0;

}
